package com.ldatum.crm.action.admin;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.ParentPackage;

import com.ldatum.crm.domain.Admin;
import com.ldatum.crm.domain.TContacts;
import com.ldatum.crm.domain.TCustomer;
import com.ldatum.crm.domain.TEmployee;
import com.ldatum.crm.service.TContactsService;
import com.ldatum.crm.service.TCustomerService;
import com.ldatum.crm.service.TEmployeeService;
import com.ldatum.crm.serviceImpl.TContactsServiceImpl;
import com.ldatum.crm.serviceImpl.TCustomerServiceImpl;
import com.ldatum.crm.serviceImpl.TEmployeeServiceImpl;

@ParentPackage("admin")

public class CustomerAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	
	private List<TCustomer> customerList = new ArrayList<TCustomer>();
	private List<TEmployee> employeeList = new ArrayList<TEmployee>();
	private List<TContacts> contactsList = new ArrayList<TContacts>();
	private List<TCustomer> iSharedCustomerList;
	private List<TCustomer> forMeSharedCustomerList;
	
	static private TCustomerService customerService = new TCustomerServiceImpl();
	private TEmployeeService employeeService = new TEmployeeServiceImpl();
	private TContactsService contactsService = new TContactsServiceImpl();
	
	private TCustomer customer = new TCustomer();
	private TContacts contacts = new TContacts();
	private TEmployee employee = new TEmployee();
	
	private int customerId;
	private int contactId;
	private String number;
	private String proprietor;
	private String fullname;
	private String abbreviation;
	private String theCurrentStage;
	private String relationGrade;
	private String typeOfCustomers;
	private String source;
	private String trade;
	private String website;
	private String systemVersion;
	private String featuresOfInterest;
	private String remarks;
	private String province;
	private String city;
	private String CountyOrdistrict;
	private String detailAddress;
	private String lxr;
	private String gender;
	private String telephone;
	private String companyPhone;
	private String isTheHighSeasCustomer;
	private String isShared;
	private String email;
	private String zipcode;
	private String address;
	private String contactsType;
	private String remarksToContacts;
	private String fax;
	private String buildDate;
	
	private String levelOne;
	private String levelTwo;
	
	private String ids;//接受需要删除的id字符串
	
	/**
	 * 公海客户
	 * @return
	 * @throws Exception
	 */
	public String highSeasCustomers() throws Exception{
		try{
			employee = employeeService.findById(TEmployee.class,(Integer)getSession(Admin.LOGIN_EMPLOYEE_ID_SESSION_NAME));
			customerList = customerService.publicCustomer(TCustomer.class);
		}catch(Exception e){
			e.printStackTrace();
		}
		levelOne = "customer";
		levelTwo = "public";
		return "listPublicCustomer";
	}
	
	/**
	 * 共享客户
	 * @return
	 * @throws Exception
	 */
	public String sharedCustomers() throws Exception{
		try{
			employee = employeeService.findById(TEmployee.class,(Integer)getSession(Admin.LOGIN_EMPLOYEE_ID_SESSION_NAME));
			iSharedCustomerList = customerService.sharedCustomer(TCustomer.class);
			forMeSharedCustomerList = customerService.sharedCustomer(TCustomer.class);
		}catch(Exception e){
			e.printStackTrace();
		}
		levelOne = "customer";
		levelTwo = "shared";
		return "listSharedCustomer";
	}

	/**
	 * 我的客户
	 * @return
	 * @throws Exception
	 */
	public String myCustomer() throws Exception{
		try {
			employee = employeeService.findById(TEmployee.class,(Integer)getSession(Admin.LOGIN_EMPLOYEE_ID_SESSION_NAME));
			customerList = customerService.myCustomer(TCustomer.class, (String)getSession(Admin.LOGIN_EMPLOYEE_USERNAME_COOKIE_NAME));
		} catch (Exception e) {
			e.printStackTrace();
		}
		levelOne = "customer";
		levelTwo = "mine";
		return "listCustomer";
	}
	/**
	 * 新建客户信息
	 * @return
	 * @throws Exception
	 */
	public String add() throws Exception{
		employeeList = employeeService.list(TEmployee.class);
		number = customerService.generateNo();
		levelOne = "customer";
		levelTwo = "mine";
		return "addCustomer";
		
	}
	/**
	 * 保存客户信息
	 * @throws Exception
	 */
	public void save() throws Exception{
		PrintWriter out = null;
		JSONObject json = new JSONObject();
		try{
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("type/html");
			response.setCharacterEncoding("utf-8");
			out = response.getWriter();
			TCustomer cus = setCustomer();
			TContacts con = setContacts();
			customerService.save(cus, con);
			json.accumulate("result", true);
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			out.println(json.toString());
			out.flush();
			out.close();
		}
	}
	/**
	 * 更新客户信息
	 * @throws Exception
	 */
	public void update() throws Exception{
		PrintWriter out = null;
		JSONObject json = new JSONObject();
		try{
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("type/html");
			response.setCharacterEncoding("utf-8");
			out = response.getWriter();
			TCustomer cus = setCustomer();
			cus.setId(customerId);
			customerService.update(cus, customerId);
			json.accumulate("result", true);
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			out.println(json.toString());
			out.flush();
			out.close();
		}
	}
	/**
	 * 可批量删除选中的客户信息
	 */
	public void deletes(){
		PrintWriter out = null;
		JSONObject json = new JSONObject();
		try{
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("type/html");
			response.setCharacterEncoding("utf-8");
			out = response.getWriter();
			TCustomer customer = new TCustomer();
			customerService.deletes(customer, ids);
			json.accumulate("result", true);
		}catch(Exception e){
			e.printStackTrace();
			json.accumulate("result", false);
		}finally{
			out.println(json.toString());
			out.flush();
			out.close();
		}
	}
	/**
	 * 查找某个客户的信息
	 * @return
	 * @throws Exception
	 */
	public String findById() throws Exception{
		employeeList = employeeService.list(TEmployee.class);
		customer = customerService.findById(TCustomer.class, customerId);
		contactsList = contactsService.findByCustomerId(customerId);
		getCustomer(customer);
		levelOne = "customer";
		levelTwo = "mine";
		return "editCustomer";
	}
	
	public void addTrackingInfo() throws Exception{
		PrintWriter out = null;
		JSONObject json = new JSONObject();
		try{
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("type/html");
			response.setCharacterEncoding("utf-8");
			out = response.getWriter();
			List<TContacts> list = contactsService.findByCustomerId(customerId); 
			for(TContacts con:list){
				con.setCustomer(null);
			}
			json.accumulate("customer", customerService.findById(TCustomer.class, customerId));
			json.accumulate("contactsList",list);
			json.accumulate("tracker", (String)getSession(Admin.LOGIN_EMPLOYEE_USERNAME_COOKIE_NAME));
			json.accumulate("result", true);
		}catch(Exception e){
			e.printStackTrace();
			json.accumulate("result", false);
		}finally{
			out.println(json.toString());
			out.flush();
			out.close();
		}
	}
	@SuppressWarnings("unused")
	private void getContacts(TContacts contacts) {
		address = contacts.getAddress();
//		number = contacts.getCusNo();
		email = contacts.getEmail();
		fax = contacts.getFax();
		gender = contacts.getGender();
		lxr = contacts.getName();
		companyPhone = contacts.getPhone();
		remarksToContacts = contacts.getRemarks();
		telephone = contacts.getTelephone();
		contactsType = contacts.getType();
		zipcode = contacts.getZipcode();
	}
	private void getCustomer(TCustomer customer2) {
		number = customer.getNumber();
		CountyOrdistrict = customer.getArea();
		detailAddress = customer.getAddress();
		typeOfCustomers = customer.getCatagory();
		city = customer.getCity();
		theCurrentStage = customer.getCurrentStep();
		featuresOfInterest = customer.getFeaturesOfInterest();
		fullname = customer.getFullname();
		systemVersion = customer.getIntentionSystemVersion();
		source = customer.getOrigin();
		proprietor = customer.getProprietor();
		province = customer.getProvince();
		relationGrade = customer.getRelationshipLevel();
		remarks = customer.getRemarks();
		abbreviation = customer.getShortName();
		trade = customer.getTrade();
		website = customer.getWebsite();
		isTheHighSeasCustomer = customer.getIsPublic();
		isShared = customer.getIsShared();
		buildDate = customer.getBuildDate();
	}
	/**
	 * 保存联系人信息到contacts中
	 * @return
	 */
	private TContacts setContacts() {
		TContacts con = new TContacts();
		con.setAddress(address);
//		con.setCusNo(number);
		con.setEmail(email);
		con.setFax(fax);
		con.setGender(gender);
		con.setName(lxr);
		con.setPhone(companyPhone);
		con.setRemarks(remarksToContacts);
		con.setTelephone(telephone);
		con.setType(contactsType);
		con.setZipcode(zipcode);
		return con;
	}
	/**
	 * 保存客户信息到customer中
	 * @return
	 */
	private TCustomer setCustomer() {
		TCustomer customer = new TCustomer();
		customer.setArea(CountyOrdistrict);
		customer.setAddress(detailAddress);
		customer.setCatagory(typeOfCustomers);
		customer.setCity(city);
		customer.setCurrentStep(theCurrentStage);
		customer.setFeaturesOfInterest(featuresOfInterest);
		customer.setFile("");
		customer.setFullname(fullname);
		customer.setIntentionSystemVersion(systemVersion);
		customer.setOrigin(source);
		customer.setProprietor(proprietor);
		customer.setProvince(province);
		customer.setRelationshipLevel(relationGrade);
		customer.setRemarks(remarks);
		customer.setShortName(abbreviation);
		customer.setTrade(trade);
		customer.setWebsite(website);
		customer.setIsPublic(isTheHighSeasCustomer);
		customer.setIsShared(isShared);
		customer.setBuildDate(buildDate);
		return customer;
	}

	public List<TCustomer> getCustomerList() {
		return customerList;
	}

	public void setCustomerList(List<TCustomer> customerList) {
		this.customerList = customerList;
	}

	public TCustomer getCustomer() {
		return customer;
	}
	public void setCustomer(TCustomer customer) {
		this.customer = customer;
	}
	public TEmployee getEmployee() {
		return employee;
	}
	public void setEmployee(TEmployee employee) {
		this.employee = employee;
	}
	public String getProprietor() {
		return proprietor;
	}
	public void setProprietor(String proprietor) {
		this.proprietor = proprietor;
	}

	public String getNumber() {
		return number;
	}

	public void setNumber(String number) {
		this.number = number;
	}

	public String getFullname() {
		return fullname;
	}

	public void setFullname(String fullname) {
		this.fullname = fullname;
	}

	public String getAbbreviation() {
		return abbreviation;
	}

	public void setAbbreviation(String abbreviation) {
		this.abbreviation = abbreviation;
	}

	public String getTheCurrentStage() {
		return theCurrentStage;
	}

	public void setTheCurrentStage(String theCurrentStage) {
		this.theCurrentStage = theCurrentStage;
	}

	public String getRelationGrade() {
		return relationGrade;
	}

	public void setRelationGrade(String relationGrade) {
		this.relationGrade = relationGrade;
	}

	public String getTypeOfCustomers() {
		return typeOfCustomers;
	}

	public void setTypeOfCustomers(String typeOfCustomers) {
		this.typeOfCustomers = typeOfCustomers;
	}

	public String getSource() {
		return source;
	}

	public void setSource(String source) {
		this.source = source;
	}

	public String getTrade() {
		return trade;
	}

	public void setTrade(String trade) {
		this.trade = trade;
	}

	public String getWebsite() {
		return website;
	}

	public void setWebsite(String website) {
		this.website = website;
	}

	public String getSystemVersion() {
		return systemVersion;
	}

	public void setSystemVersion(String systemVersion) {
		this.systemVersion = systemVersion;
	}

	public String getFeaturesOfInterest() {
		return featuresOfInterest;
	}

	public void setFeaturesOfInterest(String featuresOfInterest) {
		this.featuresOfInterest = featuresOfInterest;
	}

	public String getRemarks() {
		return remarks;
	}

	public void setRemarks(String remarks) {
		this.remarks = remarks;
	}

	public String getProvince() {
		return province;
	}

	public void setProvince(String province) {
		this.province = province;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getCountyOrdistrict() {
		return CountyOrdistrict;
	}

	public void setCountyOrdistrict(String countyOrdistrict) {
		CountyOrdistrict = countyOrdistrict;
	}

	public String getDetailAddress() {
		return detailAddress;
	}

	public void setDetailAddress(String detailAddress) {
		this.detailAddress = detailAddress;
	}

	public String getLxr() {
		return lxr;
	}

	public void setLxr(String lxr) {
		this.lxr = lxr;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	public String getTelephone() {
		return telephone;
	}

	public void setTelephone(String telephone) {
		this.telephone = telephone;
	}

	public String getCompanyPhone() {
		return companyPhone;
	}

	public void setCompanyPhone(String companyPhone) {
		this.companyPhone = companyPhone;
	}

	public String getIsTheHighSeasCustomer() {
		return isTheHighSeasCustomer;
	}

	public void setIsTheHighSeasCustomer(String isTheHighSeasCustomer) {
		this.isTheHighSeasCustomer = isTheHighSeasCustomer;
	}

	public String getIsShared() {
		return isShared;
	}

	public void setIsShared(String isShared) {
		this.isShared = isShared;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getZipcode() {
		return zipcode;
	}

	public void setZipcode(String zipcode) {
		this.zipcode = zipcode;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getRemarksToContacts() {
		return remarksToContacts;
	}

	public void setRemarksToContacts(String remarksToContacts) {
		this.remarksToContacts = remarksToContacts;
	}

	public String getContactsType() {
		return contactsType;
	}

	public void setContactsType(String contactsType) {
		this.contactsType = contactsType;
	}

	public String getFax() {
		return fax;
	}

	public void setFax(String fax) {
		this.fax = fax;
	}

	public List<TEmployee> getEmployeeList() {
		return employeeList;
	}

	public void setEmployeeList(List<TEmployee> employeeList) {
		this.employeeList = employeeList;
	}
	public int getCustomerId() {
		return customerId;
	}
	public void setCustomerId(int customerId) {
		this.customerId = customerId;
	}
	public int getContactId() {
		return contactId;
	}
	public void setContactId(int contactId) {
		this.contactId = contactId;
	}
	public String getBuildDate() {
		return buildDate;
	}
	public void setBuildDate(String buildDate) {
		this.buildDate = buildDate;
	}
	public List<TContacts> getContactsList() {
		return contactsList;
	}
	public void setContactsList(List<TContacts> contactsList) {
		this.contactsList = contactsList;
	}
	public TContacts getContacts() {
		return contacts;
	}
	public void setContacts(TContacts contacts) {
		this.contacts = contacts;
	}
	public String getIds() {
		return ids;
	}
	public void setIds(String ids) {
		this.ids = ids;
	}

	public List<TCustomer> getiSharedCustomerList() {
		return iSharedCustomerList;
	}

	public void setiSharedCustomerList(List<TCustomer> iSharedCustomerList) {
		this.iSharedCustomerList = iSharedCustomerList;
	}

	public List<TCustomer> getForMeSharedCustomerList() {
		return forMeSharedCustomerList;
	}

	public void setForMeSharedCustomerList(List<TCustomer> forMeSharedCustomerList) {
		this.forMeSharedCustomerList = forMeSharedCustomerList;
	}

	public String getLevelOne() {
		return levelOne;
	}

	public void setLevelOne(String levelOne) {
		this.levelOne = levelOne;
	}

	public String getLevelTwo() {
		return levelTwo;
	}

	public void setLevelTwo(String levelTwo) {
		this.levelTwo = levelTwo;
	}
	
}
